//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.controller.NewRegUserCommand;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.model.IServiceProxy;
	import reader.model.IUserProxy;
	import reader.model.vo.UserVO;
	import reader.utils.StringUtils;
	import reader.view.components.LoginPanel;

	public class ApplicationMediator extends BaseApplicationMediator 
										implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "ApplicationMediator";
		
		public function get airReader():Reader
		{
			return this.viewComponent as Reader;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var loginPanel:LoginPanel;
		private var userProxy:IUserProxy;
		private var serviceProxy:IServiceProxy;
		
		private var isReaderInitialized:Boolean;
		
		private var resource:XML = ReaderResource.instance.resource;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function ApplicationMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			userProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.userProxyName) as IUserProxy;
			serviceProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
			
			startUp();
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//methods
		
		public function startUp():void
		{
			facade.registerCommand(ApplicationFacade.NEW_USER_DATA, NewRegUserCommand);
						
			airReader.addEventListener(Reader.RESIZE_LABEL_TREE, onResizeLabelTree_handler);
			airReader.addEventListener(Reader.EXIT_APP, onExitApp_handler);
			
			loginPanel = LoginPanel(PopUpManager.createPopUp(airReader, LoginPanel, true));
			PopUpManager.centerPopUp(loginPanel);
			loginPanel.addEventListener(LoginPanel.APP_LOGIN, onLogin_handler);
			loginPanel.addEventListener(LoginPanel.REG_USER_NAME, onCheckUserName_handler);
			loginPanel.addEventListener(LoginPanel.REGISTER, onRegisterNew_handler);
		}
		
		public function clearReference():void
		{	
			airReader.removeEventListener(Reader.RESIZE_LABEL_TREE, onResizeLabelTree_handler);
			airReader.removeEventListener(Reader.EXIT_APP, onExitApp_handler);
			clearLoginEventListerner();
		}
		
		private function clearLoginEventListerner():void
		{
			facade.removeCommand(ApplicationFacade.NEW_USER_DATA);
			
			loginPanel.removeEventListener(LoginPanel.APP_LOGIN, onLogin_handler);
			loginPanel.removeEventListener(LoginPanel.REG_USER_NAME, onCheckUserName_handler);
			loginPanel.removeEventListener(LoginPanel.REGISTER, onRegisterNew_handler);
		}
		
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.LOGIN_SUCCESS,
					ApplicationFacade.LOGIN_FAIL,
					ApplicationFacade.LABEL_TREE_CREATE,
					ApplicationFacade.BLOG_SUMMARY_SHOW,
					ApplicationFacade.USER_IS_LABEL_NEW,
					ApplicationFacade.BLOG_FETCH,
					ApplicationFacade.REGISTER_USER_EXIT,
					ApplicationFacade.REGISTER_COMPLETE,
					ApplicationFacade.FETCH_SERVICES,
					ApplicationFacade.LOGOUT]
					);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.LOGIN_SUCCESS:
					clearLoginEventListerner();
					PopUpManager.removePopUp(loginPanel);					
					break;
				case ApplicationFacade.LOGIN_FAIL:
					loginPanel.showErrorTip(String(notification.getBody()), loginPanel.userName);
					break;
				case ApplicationFacade.LABEL_TREE_CREATE:
					if(!isReaderInitialized)
					{
						isReaderInitialized = true;						
						sendNotification(ApplicationFacade.BLOG_SUMMARY_SHOW);
					}
					break;	
				case ApplicationFacade.BLOG_SUMMARY_SHOW:
					if(airReader.blogViewStack.selectedChild != airReader.blogSummary)
					{
						airReader.blogViewStack.selectedChild = airReader.blogSummary;
					}
					break;
				case ApplicationFacade.BLOG_FETCH:
					if(airReader.blogViewStack.selectedChild != airReader.blogListBox)
					{
						airReader.blogViewStack.selectedChild = airReader.blogListBox;
					}
					break;	
				case ApplicationFacade.USER_IS_LABEL_NEW:
					userProxy.updateUserIsDisplayOldLabels(UserVO(notification.getBody()));
					break;	
				case ApplicationFacade.FETCH_SERVICES:
					readerModelLocator.services = serviceProxy.selectAllService();
					break;
				case ApplicationFacade.REGISTER_USER_EXIT:
					loginPanel.isRegNameValid = false;
					loginPanel.regUserName.errorString = resource.regUserError4;
					break;
				case ApplicationFacade.REGISTER_COMPLETE:
					var user:UserVO = notification.getBody() as UserVO;
					if(user)
					{
						sendNotification(ApplicationFacade.NEW_USER_DATA);
						PopUpManager.removePopUp(loginPanel);
						clearReference();
						sendNotification(ApplicationFacade.LOGIN_SUCCESS, readerModelLocator.airReader);
					}
					else
					{
						Alert.show(resource.regUserError6);
					}
					break;
				case ApplicationFacade.LOGOUT:
					if(loginPanel)
					{
						PopUpManager.removePopUp(loginPanel);
					}
					clearReference();
					startUp();
					break;	
			}
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onExitApp_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.CLOSE_WINDOW);
		}
		
		private function onLogin_handler(e:Event):void
		{
			var _name:String = StringUtils.trimAll(loginPanel.userName.text);
			var _pass:String = StringUtils.trimAll(loginPanel.userPass.text);
			userProxy.validate(_name, _pass);
		}
		
		private function onCheckUserName_handler(e:Event):void
		{
			var username:String = loginPanel.regUserName.text;
			userProxy.checkUserNameExit(username);
		}
		
		private function onRegisterNew_handler(e:Event):void
		{
			var user:UserVO = new UserVO();
			user.culture = readerModelLocator.culture;
			user.userName = loginPanel.regUserName.text
			user.password = loginPanel.regUserPass.text;
			user.email = loginPanel.regUserEmail.text;
			userProxy.register(user);
		}
		
		private function onResizeLabelTree_handler(e:Event):void
		{
			if(airReader.labelTree.width <= 0)
			{
				airReader.labelTree.width = 250;
			}
			else if(airReader.labelTree.width >= 0)
			{
				airReader.labelTree.width = 0;
			}
		}
	}
}